package br.unisinos.servers;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;

import br.lp2p.commons.LP2PInternalMessages;
import br.unisinos.arquivos.DirectoryEntry;
import br.unisinos.handler.Handler;

// This is the network agent

public class TcpServer implements Runnable {

    public static final int TCP_PORT = 60000;
    
    private ArrayList<DirectoryEntry> entries = null;
    
    public TcpServer(ArrayList<DirectoryEntry> entries) {
        this.entries = entries;
    }
    
    @Override
    public void run() {
        waitMessage();
    }
    
    public void waitMessage() {
        ServerSocket servSock;
   //     byte[] bBuffer; // Receive buffer

        try {
            servSock = new ServerSocket(TCP_PORT);
            
            while (true) {
   //             bBuffer = new byte[Server.MSG_BUFLEN];
                Socket clntSock = servSock.accept();
 
                clntSock.setSendBufferSize(LP2PInternalMessages.SENDF_BUFLEN);
                             
                Handler commHandler = new Handler(clntSock, entries);
                new Thread(commHandler).start();
          }
        }
        catch (IOException ioex) {
            ioex.printStackTrace();         
        }

    }
}
